import React, { useState, useEffect } from 'react'
import {Modal,message} from 'antd';
import {Link,useHistory } from 'react-router-dom'
import {
    UserOutlined,
    PoweroffOutlined,
    ExclamationCircleOutlined,
    TrophyOutlined,
} from '@ant-design/icons';
import axios from 'requset/request';
import API from "api/Api.js"

const { confirm } = Modal;

export default function TopBar(props) {
    const history = useHistory();
    const [user, setUser] = useState();
    const [curMod, setCurMode] = useState("dm");
    useEffect(() => {
        let key=sessionStorage.getItem("curMode");
        setCurMode(key);
        let user={};
       if(key =="dm"){
            user = sessionStorage.getItem('userinfo');
        }else{
            user = sessionStorage.getItem('emUser');
        }
        let userJson = JSON.parse(user);
        setUser(userJson);
    },[]);
    let fnDmFinish = () => {
        axios({
            method: 'post',
            url: API.login_out,
        }).then(res=>{
            if(res.data.code == 0){
                message.success('退出成功');
            }else{
                message.error('退出失败')
            }
           
        });
        window.location.href = './login';
        // props.history.push("./login")
    }
    let fnEmFinish = () => {
        axios({
            method: 'post',
            url: API.em_login_out,
        }).then(res=>{
            if(res.data.code == 0){
                message.success('退出成功');
            }else{
                message.error('退出失败')
            }
           
        });
        window.location.href = './login';
        // props.history.push("./login")
    }
    let loginOut=()=>{
        let curMode = sessionStorage.getItem("curMode");
        confirm({
            title: '退出提醒',
            icon: <ExclamationCircleOutlined />,
            content: '您将退出系统,请确定?',
            okText: '确定',
            okType: 'danger',
            cancelText:"取消",
            onOk:()=> {
                if(curMode =="em"){
                    fnEmFinish();
                }else{
                    fnDmFinish()
                }
              
            },
          });
    }
    let queryintegral=()=>{
        history.push("/details?userId="+user.userId);
    }
    return (
        <>
            <div className='userlist'>
                {
                    curMod=="em"&&user.roleId==6&&<span style={{marginRight:15}}><TrophyOutlined onClick={queryintegral}/></span>
                }
                <Link to='/userinfo'>
                    <UserOutlined  ></UserOutlined>
                </Link>
                <PoweroffOutlined onClick={loginOut}/>
            </div>
        </>
    )
}
